﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunSilverlight.OrganziationService;
using DunSilverlight.Comm;
using DunSilverlight.ViewModel;

namespace DunSilverlight.Views
{
    public partial class UserForm : UserControl
    {
        RoleService.RoleServiceClient roleSer = new RoleService.RoleServiceClient();
        UserService.UserServiceClient ser = new UserService.UserServiceClient();
        OrganizationServiceClient oser = new OrganizationServiceClient();
        UserModel obj = new UserModel();
        public UserForm()
        {
            InitializeComponent();
           
            this.Loaded += new RoutedEventHandler(UserForm_Loaded);
            ser.SaveOrUpdateCompleted += new EventHandler<UserService.SaveOrUpdateCompletedEventArgs>(ser_SaveOrUpdateCompleted);
            roleSer.GetRolesCompleted += new EventHandler<RoleService.GetRolesCompletedEventArgs>(roleSer_GetRolesCompleted);

            this.DataContext = obj;
            //默认启用该用户
            cboxState.IsChecked = true;
            oser.GetUserOrgCompleted += new EventHandler<GetUserOrgCompletedEventArgs>(oser_GetUserOrgCompleted);
        }

       

        void UserForm_Loaded(object sender, RoutedEventArgs e)
        {
            //生成该用户具有权限的组织机构树

            oser.GetUserOrgAsync(AppGlobal.CurrentUser.Id);


            roleSer.GetRolesAsync();
        }

        void roleSer_GetRolesCompleted(object sender, RoleService.GetRolesCompletedEventArgs e)
        {
            int total = 0;
            IList<DunLibrary.User.Role> cases = JsonSerializerHelper.JsonToEntities<DunLibrary.User.Role>(e.Result, out total);
            cboxRole.ItemsSource = cases;
        }


        void oser_GetUserOrgCompleted(object sender, GetUserOrgCompletedEventArgs e)
        {
            var root = JsonSerializerHelper.JsonToEntity<DunLibrary.Organization.Organization>(e.Result);
            if (root != null)
            {
                List<DunLibrary.Organization.Organization> list = new List<DunLibrary.Organization.Organization>();
                list.Add(root);
                bindChildren(list, root);
                cboxOrg.ItemsSource = list;

                
            }

        }
        private void bindChildren(List<DunLibrary.Organization.Organization> list, DunLibrary.Organization.Organization root)
        {
            if (root.Childs != null)
            {
                foreach (var child in root.Childs)
                {
                    list.Add(child);
                    bindChildren(list, child);
                }
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            //if (string.IsNullOrEmpty(obj.Name))
            //{
            //    Tools.ShowMessage("名字不能为空", "", false);
            //    return;
            //}
            //if (string.IsNullOrEmpty(tbPad.Password) || string.IsNullOrEmpty(tbPadConfirm.Password))
            //{
            //    Tools.ShowMessage("密码不能为空，请重新输入", "", false);
            //    tbPad.Password = "";
            //    tbPadConfirm.Password = "";
            //    tbPad.Focus();
            //    return;
            //}
            //if (tbPad.Password != tbPadConfirm.Password)
            //{
            //    Tools.ShowMessage("密码不一致，请重新输入", "", false);
            //    tbPad.Password = "";
            //    tbPadConfirm.Password = "";
            //    tbPad.Focus();
            //    return;
            //}

            //if (cboxOrg.SelectedItem == null)
            //{
            //    Tools.ShowMessage("请选择机构", "", false);
            //    return;
            //}

            //if (obj.Number == null)
            //    obj.Number = string.Empty;
            //obj.Organization.Id = (cboxOrg.SelectedItem as DunLibrary.Organization.Organization).Id;
            //if (cboxState.SelectedItem != null)
            //{
            //    obj.IsUsed = cboxState.SelectedValue.ToString() == "启用" ? true : false;
            //}
            if (tbPad.Password != tbPadConfirm.Password)
            {
                Tools.ShowMessage("密码不一致，请重新输入", "", false);
                tbPad.Password = "";
                tbPadConfirm.Password = "";
                tbPad.Focus();
                return;
            }
            if (obj.Validation())
            {
                var entity = obj.ToEntity();
                ser.SaveOrUpdateAsync(JsonSerializerHelper.EntityToJson(entity));
            }
            else
            {
                Tools.ShowMessage("输入格式不正确", "", false);
            }
           

           
        }

        void ser_SaveOrUpdateCompleted(object sender, UserService.SaveOrUpdateCompletedEventArgs e)
        {
            var user = JsonSerializerHelper.JsonToEntity<DunLibrary.User.User>(e.Result);
            if (user != null)
                (this.Parent as RadWindow).Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }
    }
}
